import widgetService from '@/api/widgetService'

let widgets
export async function pushByWidgetName (widgetName, vm, param, appName) {
  const res = await getWidgets()
  const keys = Object.keys(res)
  for (var i = 0; i < keys.length; i++) {
    if (appName === keys[i]) {
      const widgets = res[keys[i]]
      const findItem = widgets.find(o => o.name === widgetName)
      if (findItem) {
        vm.$router.push({
          name: findItem.routerName,
          ...param
        })
      }
    }
  }
}

async function getWidgets () {
    return widgets ||
      (widgets = await widgetService.listCustomWidgetWithOutBtnByAPPNames(window.config.VUE_APP_NAME))
}
